// createRouter：创建router实例对象
// createWebHistory：创建history模式的路由

import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/user/Login/index.vue'
import Layout from '@/views/user/Layout/index.vue'
import Home from '@/views/user/Home/index.vue'
import Category from '@/views/user/Category/index.vue'
import SubCategory from '@/views/user/SubCategory/index.vue'
import Goods from '@/views/user/Goods/index.vue'
import CartList from '@/views/user/CartList/index.vue'
import Order from '@/views/user/Order/index.vue'
import Pay from '@/views/user/Pay/index.vue'
import PayBack from '@/views/user/Pay/PayBack.vue'
import Member from '@/views/user/Member/index.vue'
import UserInfo from '@/views/user/Member/components/UserInfo.vue'
import UserOrder from '@/views/user/Member/components/UserOrder.vue'
import AdminLayout from '@/views/admin/Layout/index.vue'
import AdminLogin from '@/views/admin/Login/index.vue'
import WorkSpace from '@/views/admin/WorkSpace/index.vue'
import AdminBanner from '@/views/admin/Banner/index.vue'
import AdminCategory from '@/views/admin/Category/index.vue'
import AdminSubCategory from '@/views/admin/SubCategory/index.vue'
import AdminGoods from '@/views/admin/Goods/index.vue'
import AdminSpec from '@/views/admin/Spec/index.vue'
import AdminSpecValue from '@/views/admin/SpecValue/index.vue'
import AdminOrder from '@/views/admin/Order/index.vue'
import User from '@/views/admin/User/index.vue'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // path和component对应关系的位置
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '',
          component: Home
        },
        {
          path: 'category/:id',
          component: Category
        },
        {
          path: 'category/sub/:id',
          component: SubCategory
        },
        {
          path: 'goods/:id',
          component: Goods
        },
        {
          path: 'cartlist',
          component: CartList
        },
        {
          path: 'order',
          component: Order
        },
        {
          path: 'pay',
          component: Pay
        },
        {
          path: '/pay/result',
          component: PayBack
        },
        {
          path: 'member',
          component: Member,
          children: [
            {
              path: '',
              component: UserInfo
            },
            {
              path: 'order',
              component: UserOrder
            }
          ]
        }
      ]
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/admin',
      component: AdminLayout,
      children: [
        {
          path: '',
          component: WorkSpace
        },
        {
          path: '/admin/banner',
          component: AdminBanner
        },
        {
          path: '/admin/category',
          component: AdminCategory
        },
        {
          path: '/admin/subcategory',
          component: AdminSubCategory
        },
        {
          path: '/admin/goods',
          component: AdminGoods
        },
        {
          path: '/admin/spec',
          component: AdminSpec
        },
        {
          path: '/admin/spec/value',
          component: AdminSpecValue
        },
        {
          path: '/admin/order',
          component: AdminOrder
        },
        {
          path: '/admin/user',
          component: User
        },
      ]
      },
      {
        path: '/admin/login',
        component: AdminLogin
      },
  ],
  // 路由滚动行为定制
  scrollBehavior () {
    return {
      top: 0
    }
  }
})

// 设置全局路由守卫
router.beforeEach((to, from, next) => {
  // 根据路径判断设置标题
  if (to.path.startsWith('/admin')) {
    document.title = '星贸商城管理系统';  // 管理系统页面的标题
  } else {
    document.title = '星贸商城';  // 商城相关页面的标题
  }
  next();  // 继续导航
});

export default router
